package pr.iceworld.fernando.apigateway.application.service;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import pr.iceworld.fernando.apigateway.application.model.UserDetail;

import java.util.HashMap;
import java.util.Map;

@Component
/**
 * @RequiredArgsConstructor 针对 loadBalancedRestTemplate final 的 初始化
 */
@RequiredArgsConstructor
public class UserServiceClient {

    private final RestTemplate loadBalancedRestTemplate;

    public UserDetail getUserDetail(final String username) {
        return loadBalancedRestTemplate.getForObject("http://provider-user-service/users/{username}", UserDetail.class, username);
    }

    public PageInfo<UserDetail> getUserDetails(final String dateStart, final String dateEnd) {
        Map<String, String> params = new HashMap<>();
        params.put("dateStart", dateStart);
        params.put("dateEnd", dateEnd);
        //return loadBalancedRestTemplate.getForObject("http://provider-user-service/users/findAll?dateStart={dateStart}&dateEnd={dateEnd}", List.class, dateStart, dateEnd);
        return loadBalancedRestTemplate.getForObject("http://provider-user-service/users/findAll?dateStart={dateStart}&dateEnd={dateEnd}", PageInfo.class, params);
    }

}
